package com.example.fragmentest;

        import androidx.appcompat.app.AppCompatActivity;


        import android.content.Intent;
        import android.content.SharedPreferences;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.Toast;

        import com.example.fragmentest.Beam.User;

        import org.litepal.LitePal;

        import java.util.ArrayList;
        import java.util.List;


public class LoginActivity extends AppCompatActivity {
private EditText usnedit,psdedit;
private Button registerbtn,loginbtn;
List<User> userList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getUser();
        initView();
        setListener();
    }

    private void getUser() {
        List<User> userList= LitePal.findAll(User.class);
        if (userList.size()==0){
            User user=new User(0,"张三","123");
            User user1=new User(0,"李四","456");
            User user2=new User(0,"王五","789");
            user.save();
            user1.save();
            user2.save();
        }
    }

    private void setListener() {
        registerbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String usn=usnedit.getText().toString().trim();
                String psd=psdedit.getText().toString().trim();
                User user=new User(0,usn,psd);
                if (user.save()){
                    showToast("注册成功");
                }else {
                    showToast("用户已存在，请登录");
                }
            }
        });
        loginbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String usn=usnedit.getText().toString().trim();
                String psd=psdedit.getText().toString().trim();
                User user=confirmUser(usn,psd);
                if (user==null){
                    showToast("用户名密码错误");
                }else{
                    SharedPreferences sharedPreferences=getSharedPreferences("userinfo",MODE_PRIVATE);
                    SharedPreferences.Editor editor=sharedPreferences.edit();
                    editor.putInt("userid",user.getId());
                    editor.putString("username",user.getName());
                    editor.putString("password",user.getPsd());
                    editor.apply();
                    showToast("登陆成功");
                    Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        });

    }
    private void showToast(String str){
        Toast.makeText(LoginActivity.this,str,Toast.LENGTH_SHORT).show();
    }
private  User confirmUser(String usn,String psd){
        userList=LitePal.findAll(User.class);
        for (User u:userList){
            if (usn.equals(u.getName())&&psd.equals(u.getPsd())){
                return  u;
            }
        }
        return  null;
}
    private void initView() {
        usnedit=findViewById(R.id.usnedit);
        psdedit=findViewById(R.id.psdedit);
        registerbtn=findViewById(R.id.registerbtn);
        loginbtn=findViewById(R.id.loginbtn);
    }
}
